﻿using System;
using System.ComponentModel;
using Microsoft.Phone.Shell;

namespace WP7AppLifecycleServiceApp
{
    /// <summary>
    /// ViewModel class for the sample app using the Wp7AppLifecycleServiceLib.
    /// </summary>
    public class ViewModel : INotifyPropertyChanged, WP7AppLifecycleService.IApplicationStateHandler
    {
        #region Fields
        
        /// <summary>
        /// Holds the text associated to the data model.
        /// </summary>
        private string text;

        #endregion Fields

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        /// <summary>
        /// Gets or sets the text associated to the "data" model.
        /// </summary>
        public string Text
        {
            get 
            { 
                return this.text; 
            }
            
            set 
            {
                if (value != this.text)
                {
                    this.text = value;
                    this.NotifyPropertyChanged("Text");
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether there is application state saved.
        /// </summary>
        /// <value>A bool value indicating whether there is application state saved.</value>
        /// <remarks>Take into account the situation where the app was tombstoned
        /// before initialization was completed. If the stored data are not 
        /// fully complete return false.</remarks>
        public bool IsThereApplicationStateSaved
        {
            get
            {
                return PhoneApplicationService.Current.State.Keys.Contains("viewModel.text");
            }
        }
        
        #endregion Properties

        #region interface IApplicationStateHandler

        /// <summary>
        /// Saves the data of the application when the application is tombstoned.
        /// </summary>
        public void SaveApplicationState()
        {
            PhoneApplicationService.Current.State["viewModel.text"] = this.Text;
        }

        /// <summary>
        /// Restores the data of the application when the application comes back to life
        /// after having been tombstoned.
        /// </summary>
        public void RestoreApplicationState()
        {
            object value = null;
            if (PhoneApplicationService.Current.State.TryGetValue("viewModel.text", out value))
            {
                this.Text = value as string;
            }
        }

        /// <summary>
        /// Deletes any state saved when the application was tombstoned. 
        /// <para>This method will automatically be called right after RestoreApplicationState is
        /// successfully executed.</para>
        /// </summary>
        public void DeleteApplicationState()
        {
            PhoneApplicationService.Current.State.Remove("viewModel.text");
        }

        #endregion interface IApplicationStateHandler

        /// <summary>
        /// Notifies that property value changed.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
